<template>
  <el-dialog
    :title="!dataForm.id ? '转账' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="收款账户" prop="targetAccountName">
      <el-input v-model="dataForm.targetAccountName" placeholder="收款人账户名"></el-input>
    </el-form-item>
    <el-form-item label="收款性名" prop="targetAccountRealname">
      <el-input v-model="dataForm.targetAccountRealname" placeholder="收款人姓名"></el-input>
    </el-form-item>
    <el-form-item label="转账金额" prop="accountflowMoney">
      <el-input v-model="dataForm.accountflowMoney" placeholder="转账金额"></el-input>
    </el-form-item>
    <el-form-item label="账户密码" prop="accountPassword">
      <el-input v-model="dataForm.accountPassword" type="password" placeholder="账户密码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          targetAccountName: '',
          targetAccountRealname:'',
          accountflowMoney: '',
          accountPassword:''
        },
        dataRule: {
          // accountflowTime: [
          //   { required: true, message: '交易时间不能为空', trigger: 'blur' }
          // ],
          targetAccountName: [
            { required: true, message: '收款人账户名不能为空', trigger: 'blur' }
          ],
          targetAccountRealname: [
            { required: true, message: '收款人性名不能为空', trigger: 'blur' }
          ],
          accountflowMoney: [
            { required: true, message: '转账金额不能为空', trigger: 'blur' }
          ],
          accountPassword: [
            { required: true, message: '账户密码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.accountflowId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.accountflowId) {
            this.$http({
              url: this.$http.adornUrl(`/expect/accountflow/info/${this.dataForm.accountflowId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.targetAccountName = data.accountflow.targetAccountName
                this.dataForm.targetAccountRealname = data.accountflow.targetAccountRealname
                this.dataForm.accountflowMoney = data.accountflow.accountflowMoney
                this.dataForm.accountPassword = data.accountflow.accountPassword
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/efinance/account/${!this.dataForm.accountflowId ? 'transfer' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'targetAccountName': this.dataForm.targetAccountName,
                'targetAccountRealname': this.dataForm.targetAccountRealname,
                'accountflowMoney': this.dataForm.accountflowMoney,
                'accountPassword': this.dataForm.accountPassword,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
